﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类t_Linkage_Commision。
    /// </summary>
    public class t_Linkage_Commision
    {
        public t_Linkage_Commision()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.t_Linkage_Commision model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Linkage_Commision(");
            strSql.Append("RecordId,UserId,UserName,UserCode,DepartId,DepartName,ItemId,ItemName,CustomerName,Ridgepole,Floor,Room,Area,BargainTime,BargainUnit,BargainTotal,SignName,SignTime,SignTotal,SupplyCommisionScale,Achievement,EmpCommisionScale,EmpCommision,DZCommisionScale,DZCommision,ZJCommisionScale,ZJCommision,Point,AchievementDate)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@UserId,@UserName,@UserCode,@DepartId,@DepartName,@ItemId,@ItemName,@CustomerName,@Ridgepole,@Floor,@Room,@Area,@BargainTime,@BargainUnit,@BargainTotal,@SignName,@SignTime,@SignTotal,@SupplyCommisionScale,@Achievement,@EmpCommisionScale,@EmpCommision,@DZCommisionScale,@DZCommision,@ZJCommisionScale,@ZJCommision,@Point,@AchievementDate)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "UserCode", DbType.AnsiString, model.UserCode);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "ItemId", DbType.Guid, model.ItemId);
            db.AddInParameter(dbCommand, "ItemName", DbType.AnsiString, model.ItemName);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Ridgepole", DbType.AnsiString, model.Ridgepole);
            db.AddInParameter(dbCommand, "Floor", DbType.AnsiString, model.Floor);
            db.AddInParameter(dbCommand, "Room", DbType.AnsiString, model.Room);
            db.AddInParameter(dbCommand, "Area", DbType.Decimal, model.Area);
            db.AddInParameter(dbCommand, "BargainTime", DbType.DateTime, model.BargainTime);
            db.AddInParameter(dbCommand, "BargainUnit", DbType.Decimal, model.BargainUnit);
            db.AddInParameter(dbCommand, "BargainTotal", DbType.Decimal, model.BargainTotal);
            db.AddInParameter(dbCommand, "SignName", DbType.AnsiString, model.SignName);
            db.AddInParameter(dbCommand, "SignTime", DbType.DateTime, model.SignTime);
            db.AddInParameter(dbCommand, "SignTotal", DbType.Decimal, model.SignTotal);
            db.AddInParameter(dbCommand, "SupplyCommisionScale", DbType.Decimal, model.SupplyCommisionScale);
            db.AddInParameter(dbCommand, "Achievement", DbType.Decimal, model.Achievement);
            db.AddInParameter(dbCommand, "EmpCommisionScale", DbType.Decimal, model.EmpCommisionScale);
            db.AddInParameter(dbCommand, "EmpCommision", DbType.Decimal, model.EmpCommision);
            db.AddInParameter(dbCommand, "DZCommisionScale", DbType.Decimal, model.DZCommisionScale);
            db.AddInParameter(dbCommand, "DZCommision", DbType.Decimal, model.DZCommision);
            db.AddInParameter(dbCommand, "ZJCommisionScale", DbType.Decimal, model.ZJCommisionScale);
            db.AddInParameter(dbCommand, "ZJCommision", DbType.Decimal, model.ZJCommision);
            db.AddInParameter(dbCommand, "Point", DbType.Int32, model.Point);
            db.AddInParameter(dbCommand, "AchievementDate", DbType.DateTime, model.AchievementDate);            
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.t_Linkage_Commision model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Linkage_Commision set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("UserCode=@UserCode,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("DepartName=@DepartName,");
            strSql.Append("ItemId=@ItemId,");
            strSql.Append("ItemName=@ItemName,");
            strSql.Append("CustomerName=@CustomerName,");
            strSql.Append("Ridgepole=@Ridgepole,");
            strSql.Append("Floor=@Floor,");
            strSql.Append("Room=@Room,");
            strSql.Append("Area=@Area,");
            strSql.Append("BargainTime=@BargainTime,");
            strSql.Append("BargainUnit=@BargainUnit,");
            strSql.Append("BargainTotal=@BargainTotal,");
            strSql.Append("SignName=@SignName,");
            strSql.Append("SignTime=@SignTime,");
            strSql.Append("SignTotal=@SignTotal,");
            strSql.Append("SupplyCommisionScale=@SupplyCommisionScale,");
            strSql.Append("Achievement=@Achievement,");
            strSql.Append("EmpCommisionScale=@EmpCommisionScale,");
            strSql.Append("EmpCommision=@EmpCommision,");
            strSql.Append("DZCommisionScale=@DZCommisionScale,");
            strSql.Append("DZCommision=@DZCommision,");
            strSql.Append("ZJCommisionScale=@ZJCommisionScale,");
            strSql.Append("ZJCommision=@ZJCommision,");
            strSql.Append("Point=@Point,");
            strSql.Append("AchievementDate=@AchievementDate");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "UserCode", DbType.AnsiString, model.UserCode);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "ItemId", DbType.Guid, model.ItemId);
            db.AddInParameter(dbCommand, "ItemName", DbType.AnsiString, model.ItemName);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Ridgepole", DbType.AnsiString, model.Ridgepole);
            db.AddInParameter(dbCommand, "Floor", DbType.AnsiString, model.Floor);
            db.AddInParameter(dbCommand, "Room", DbType.AnsiString, model.Room);
            db.AddInParameter(dbCommand, "Area", DbType.Decimal, model.Area);
            db.AddInParameter(dbCommand, "BargainTime", DbType.DateTime, model.BargainTime);
            db.AddInParameter(dbCommand, "BargainUnit", DbType.Decimal, model.BargainUnit);
            db.AddInParameter(dbCommand, "BargainTotal", DbType.Decimal, model.BargainTotal);
            db.AddInParameter(dbCommand, "SignName", DbType.AnsiString, model.SignName);
            db.AddInParameter(dbCommand, "SignTime", DbType.DateTime, model.SignTime);
            db.AddInParameter(dbCommand, "SignTotal", DbType.Decimal, model.SignTotal);
            db.AddInParameter(dbCommand, "SupplyCommisionScale", DbType.Decimal, model.SupplyCommisionScale);
            db.AddInParameter(dbCommand, "Achievement", DbType.Decimal, model.Achievement);
            db.AddInParameter(dbCommand, "EmpCommisionScale", DbType.Decimal, model.EmpCommisionScale);
            db.AddInParameter(dbCommand, "EmpCommision", DbType.Decimal, model.EmpCommision);
            db.AddInParameter(dbCommand, "DZCommisionScale", DbType.Decimal, model.DZCommisionScale);
            db.AddInParameter(dbCommand, "DZCommision", DbType.Decimal, model.DZCommision);
            db.AddInParameter(dbCommand, "ZJCommisionScale", DbType.Decimal, model.ZJCommisionScale);
            db.AddInParameter(dbCommand, "ZJCommision", DbType.Decimal, model.ZJCommision);
            db.AddInParameter(dbCommand, "Point", DbType.Int32, model.Point);
            db.AddInParameter(dbCommand, "AchievementDate", DbType.DateTime, model.AchievementDate); 
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Linkage_Commision ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Linkage_Commision GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,UserCode,DepartId,DepartName,ItemId,ItemName,CustomerName,Ridgepole,Floor,Room,Area,BargainTime,BargainUnit,BargainTotal,SignName,SignTime,SignTotal,SupplyCommisionScale,Achievement,EmpCommisionScale,EmpCommision,DZCommisionScale,DZCommision,ZJCommisionScale,ZJCommision,Point,AchievementDate from t_Linkage_Commision ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.t_Linkage_Commision model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,UserCode,DepartId,DepartName,ItemId,ItemName,CustomerName,Ridgepole,Floor,Room,Area,BargainTime,BargainUnit,BargainTotal,SignName,SignTime,SignTotal,SupplyCommisionScale,Achievement,EmpCommisionScale,EmpCommision,DZCommisionScale,DZCommision,ZJCommisionScale,ZJCommision,Point,AchievementDate ");
            strSql.Append(" FROM t_Linkage_Commision ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_Linkage_Commision");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.t_Linkage_Commision> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,UserCode,DepartId,DepartName,ItemId,ItemName,CustomerName,Ridgepole,Floor,Room,Area,BargainTime,BargainUnit,BargainTotal,SignName,SignTime,SignTotal,SupplyCommisionScale,Achievement,EmpCommisionScale,EmpCommision,DZCommisionScale,DZCommision,ZJCommisionScale,ZJCommision,Point,AchievementDate ");
            strSql.Append(" FROM t_Linkage_Commision ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.t_Linkage_Commision> list = new List<Chenxin.Lib.Business.Entity.t_Linkage_Commision>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        //执行SQL
        public static bool ExecSql(string strSql)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            int count = db.ExecuteNonQuery(CommandType.Text, strSql.ToString());
            if (count > 0)
                return true;
            else
                return false;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Linkage_Commision ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.t_Linkage_Commision model = new Chenxin.Lib.Business.Entity.t_Linkage_Commision();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["UserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UserId = new Guid(ojb.ToString());
            }
            model.UserName = dataReader["UserName"].ToString();
            model.UserCode = dataReader["UserCode"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.DepartName = dataReader["DepartName"].ToString();
            ojb = dataReader["ItemId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ItemId = new Guid(ojb.ToString());
            }
            model.ItemName = dataReader["ItemName"].ToString();
            model.CustomerName = dataReader["CustomerName"].ToString();
            model.Ridgepole = dataReader["Ridgepole"].ToString();
            model.Floor = dataReader["Floor"].ToString();
            model.Room = dataReader["Room"].ToString();
            ojb = dataReader["Area"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Area = (decimal)ojb;
            }
            ojb = dataReader["BargainTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BargainTime = (DateTime)ojb;
            }
            ojb = dataReader["BargainUnit"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BargainUnit = (decimal)ojb;
            }
            ojb = dataReader["BargainTotal"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BargainTotal = (decimal)ojb;
            }
            model.SignName = dataReader["SignName"].ToString();
            ojb = dataReader["SignTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SignTime = (DateTime)ojb;
            }
            ojb = dataReader["SignTotal"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SignTotal = (decimal)ojb;
            }
            ojb = dataReader["SupplyCommisionScale"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SupplyCommisionScale = (decimal)ojb;
            }
            ojb = dataReader["Achievement"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Achievement = (decimal)ojb;
            }
            ojb = dataReader["EmpCommisionScale"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.EmpCommisionScale = (decimal)ojb;
            }
            ojb = dataReader["EmpCommision"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.EmpCommision = (decimal)ojb;
            }
            ojb = dataReader["DZCommisionScale"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DZCommisionScale = (decimal)ojb;
            }
            ojb = dataReader["DZCommision"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DZCommision = (decimal)ojb;
            }
            ojb = dataReader["ZJCommisionScale"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ZJCommisionScale = (decimal)ojb;
            }
            ojb = dataReader["ZJCommision"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ZJCommision = (decimal)ojb;
            }
            ojb = dataReader["Point"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Point = (int)ojb;
            }
            ojb = dataReader["AchievementDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AchievementDate = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}
